/**
***********************************************************************************************************************
 * Copyright (c) 2025
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * @file        loop.c
 *
 * @revision
 *          Date:           2025/4/10
 *          Author:         Damon
 *          Notes:
 * @description
 *          包含了两种函数指针遍历调度的方法：
 *          1. 基于 export 机制的初始化和轮询调度
 *          2. 直接使用函数指针列表进行轮询调度
 *
 ***********************************************************************************************************************
 */
#include "loop.h"
#include <stdio.h>
#include "config.h"



LoopFunc init_fun_list[MAX_LIST_LEN];
LoopFunc loop_fun_list[MAX_LIST_LEN];
int fun_init_cnt;
int fun_loop_cnt;



void _init_fun_list() {
    for (int i = 0; i < fun_init_cnt; i++) {
        init_fun_list[i]();
    }
}

void _loop_fun_list() {
    for (int i = 0; i < fun_loop_cnt; i++) {
        loop_fun_list[i]();
    }
}

#ifdef USING_TEST1
// 模块的使用,自定义的函数功能
INIT_FUNCTION(init_test1) {
    printf("test1 Module init\n");
}

LOOP_FUNCTION(run_test1) {
    printf("test1 Module loop\n");
}

#endif
#ifdef USING_TEST2
LOOP_FUNCTION(run_test2) {
    printf("test2 Module loop\n");
}
#endif

// 延迟休眠函数
static void _sleep(int val) {
    // volatile int a;
    // a = a;
}

void init_func(void) {
    _init_fun_list();
    _sleep(50); // 延时 50ms 防止不同设备之间有冲突
}

void loop_func(void) {
    while (1) {
        _loop_fun_list();
        _sleep(1000); // 延时函数 1s 后执行下一个函数
    }
}

void test(void) {
}
